import store from '@/store/index';

import {InterfaceUser} from '@/interface/InterfaceUser';


export default class ChatMessageEntity {
    public needSaving: boolean = false;

    private readonly messageData: any;
    private readonly targetData: any;
    private messageType: string[] = ['save_text', 'save_img', 'gift', 'text', 'say_img', 'say'];
    // @ts-ignore
    private user: InterfaceUser = store.state.UserStore;

    public constructor(messageData: any, targetData: any) {
        this.messageData = messageData;
        this.targetData = targetData;
    }

    public get identification(): string {
        return this.messageData.data.identification;
    }

    public get isChatMessage(): boolean {
        const type = this.messageData.type || 'unknown';
        return this.messageType.indexOf(type.toLowerCase()) >= 0;
    }

    public get chatMessageData(): any {
        switch (this.messageData.type) {
            case 'save_text':
                this.needSaving = true;
                return this.messageItemDataExtractKey(this.messageIsSendTextHandle);
                break;
            case 'say':
                this.needSaving = false;
                return this.messageItemDataExtractKey(this.messageIsAcceptTextHandle);
                break;
            case 'save_img':
                this.needSaving = true;
                return this.messageItemDataExtractKey(this.messageIsSendImageHandle);
                break;
            case 'say_img':
                this.needSaving = false;
                return this.messageItemDataExtractKey(this.messageIsAcceptHandle);
                break;
            case 'gift':
                this.needSaving = false;
                return this.messageItemDataExtractKey(this.messageIsGiftHandle);
                break;

            default:
                return {};
        }
        return {};
    }

    public get saveParameter(): any {
        return {
            fromid: this.messageData.data.fromid,
            toid: this.messageData.data.toid,
            content: this.messageData.type === 'save_img' ? this.messageData.data.content.id : this.messageData.data.content,
            time: this.messageData.data.time,
            type: this.messageData.data.type,
            identification: this.messageData.data.identification,
        };
    }

    private get messageIsSendTextHandle(): any {
        return {
            type: this.messageData.data.type,
            fromId: this.messageData.data.fromid,
            toId: this.messageData.data.toid,
            avatar: this.avatar,
            text: this.messageData.data.content,
            abnormal: 1,
            timeStamp: this.messageData.data.time,
            isSelf: this.isSelf,
            identification: this.messageData.data.identification,
        };
    }

    private get messageIsAcceptTextHandle(): any {
        const messageItemData: any = this.messageIsSendTextHandle;
        return Object.assign(messageItemData, {abnormal: 0});
    }

    private get messageIsSendImageHandle(): any {
        return {
            type: this.messageData.data.type,
            fromId: this.messageData.data.fromid,
            toId: this.messageData.data.toid,
            avatar: this.avatar,
            text: this.messageData.data.content.file_path,
            abnormal: 1,
            timeStamp: this.messageData.data.time,
            isSelf: this.isSelf,
            identification: this.messageData.data.identification,
        };
    }

    private get messageIsAcceptHandle(): any {
        const messageItemData: any = this.messageIsSendImageHandle;
        return Object.assign(messageItemData, {abnormal: 0});
    }

    private get messageIsGiftHandle(): any {
        return {
            type: 101,
            fromId: this.messageData.data.fromid,
            toId: this.messageData.data.toid,
            avatar: this.avatar,
            text: this.messageData.data.gift,
            abnormal: 0,
            timeStamp: this.messageData.data.time,
            isSelf: this.isSelf,
            identification: this.messageData.data.identification,
        };
    }

    private get avatar(): string {
        return this.messageData.data.fromid === this.user.userId ? this.user.avatar.filePath : this.targetData.avatar.file_path;
    }

    private get isSelf(): boolean {
        return this.messageData.data.fromid === this.user.userId;
    }

    private messageItemDataExtractKey(item: any): any {
        const obj: any = {};
        obj[item.identification] = item;
        // obj[item.identification].text = this.replaceContent(obj[item.identification].text);
        return obj;
    }
}
